package service;

import dao.BtcfrelationDao;
import dao.ShangHanTiaoBianDao;
import dto.ShtbDetailDto;
import pojo.Btcfrelation;
import pojo.ShangHanTiaoBian;
import pojo.Tcm;
import util.PageUtil;

import java.util.List;

/**
 *
 * @author wxr
 *
 */
public class ShtbService {
    private ShangHanTiaoBianDao shtbdao = new ShangHanTiaoBianDao();
    private BtcfrelationDao btdao = new BtcfrelationDao();
    //条辩信息展示
    public PageUtil getShtbList(int currentPage) {
        //        设置一页的记录数
        int pageSize=3;
//        获取总记录数
        int totalNum = shtbdao.getTotalNum();
        PageUtil<ShangHanTiaoBian> pu = new PageUtil<>(currentPage, pageSize, totalNum);
        //查当前页数据
        List<ShangHanTiaoBian> list = shtbdao.getShangHanTiaoBianList(pu.getCurrentPage(),pu.getPageSize());
        //将查询到的指定页码的数据存储到分页工具对象中
        pu.setList(list);
        //将分页工具对象返回
        return pu;
    }


    //条辩信息添加
    public void addShtb(ShangHanTiaoBian shtb) {
        //查看编号是否在表中
      /*  ShangHanTiaoBian shtb1 = shtbdao.getShtb(shtb.getNum());
        //不存在则插入数据
        if(shtb1==null){*/
            shtbdao.add(shtb);
    }

    //条辩信息更新
    public void updateShtb(ShangHanTiaoBian shtb) {
        shtbdao.update(shtb);
    }

    //条辩信息删除
    public void deleteShtb(int id) {
        shtbdao.delete(id);
    }

    public  ShangHanTiaoBian getShtb(int id) {
        return shtbdao.getShtb(id);
    }

    public PageUtil getQueryShtbList(String key, int flag){
//        key：搜索关键字
        List<ShangHanTiaoBian> list =shtbdao.getQueryShtbList(key,flag);
        int pageSize=3;
        int totalNum=list.size();
        PageUtil<ShangHanTiaoBian> pu = new PageUtil<>(1, pageSize, totalNum);
        pu.setList(list);
        System.out.println(pu.getList());
        return pu;
    }


    //    获取详细信息，封装为dto
    public ShtbDetailDto getShtbDetail(int tbid){
        ShtbDetailDto detailDto = new ShtbDetailDto();
        ShangHanTiaoBian shtb = shtbdao.getDetail(tbid);
        List<Btcfrelation> btcfrelationList = btdao.getBtcfrelationList(tbid);
        detailDto.setNum(shtb.getNum());
        detailDto.setContent(shtb.getContent());
        detailDto.setComment(shtb.getComment());
        detailDto.setBtcfRelationList(btcfrelationList);
        return detailDto;
    }
}
